package com.bawei.liuzhanlin20210806.view.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bawei.liuzhanlin20210806.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jzvd.JzvdStd;
import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class Main3Activity extends AppCompatActivity {

    @BindView(R.id.jz)
    JzvdStd jz;
    @BindView(R.id.dan)
    DanmakuView dan;
    @BindView(R.id.et_nr)
    EditText etNr;
    @BindView(R.id.but_fs)
    Button butFs;
    @BindView(R.id.cb)
    CheckBox cb;
    @BindView(R.id.tv_fh)
    TextView tvFh;
    private DanmakuContext danmakuContext;
    private BaseDanmakuParser danmakuParser = new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String sp = intent.getStringExtra("sp");
        sp = "http://172.17.27.50/moni/zg6/video/001.mp4";
        jz.setUp(sp, null);
        //是否隐藏弹幕
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    init();
                    getLisenter();
                } else {
                    dan.release();
                }
            }
        });
        //点击输入框添加弹幕
        butFs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nr = etNr.getText().toString();
                addDanmu(nr, true);
                etNr.setText(null);
            }
        });
        //点击返回
        tvFh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(Main3Activity.this)
                        .setTitle("提示")
                        .setMessage("是否确定退出")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                onBackPressed();
                            }
                        })
                        .setNegativeButton("取消",null)
                        .show();
            }
        });

    }

    private void init() {
        dan.enableDanmakuDrawingCache(true);
        danmakuContext = DanmakuContext.create();
        danmakuContext.setScaleTextSize(1.0f);
        dan.prepare(danmakuParser, danmakuContext);
    }
    //自定义弹幕  弹幕的准备
    private void getLisenter() {
        dan.setCallback(new DrawHandler.Callback() {
            @Override
            public void prepared() {
                dan.start();

                addDanmu("111", false);
                addDanmu("456", false);
                addDanmu("789", false);

            }

            @Override
            public void updateTimer(DanmakuTimer timer) {

            }

            @Override
            public void danmakuShown(BaseDanmaku danmaku) {

            }

            @Override
            public void drawingFinished() {

            }
        });
    }
    //添加弹幕
    private void addDanmu(String content, boolean isFase) {
        BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        danmaku.text = content;
        danmaku.padding = 5;
        danmaku.textSize = 30;
        danmaku.setTime(dan.getCurrentTime());
        danmaku.textColor = Color.RED;
        if (isFase) {
            danmaku.borderColor = Color.YELLOW;
        }
        dan.addDanmaku(danmaku);
    }
}
